package com.jye.cloud.user.service.impl;


import com.jye.cloud.Constants.MyConstants;
import com.jye.cloud.domain.Comments;
import com.jye.cloud.domain.CommentsReply;
import com.jye.cloud.user.mapper.CommentsReplyMapper;
import com.jye.cloud.user.mapper.CommentsMapper;
import com.jye.cloud.user.service.CommentsService;
import com.jye.cloud.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    CommentsMapper commentsMapper;

    @Autowired
    CommentsReplyMapper commentReplyMapper;

    @Autowired
    UserMapper userMapper;


    @Override
    public int addComment(Comments comments) {
        try {
            commentsMapper.addComment(comments);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }


    @Override
    public int updateCommentStatusById(int id, int status) {
        try {

        } catch (Exception e) {

        }
        return 1;
    }


    @Override
    public List<Comments> findCommentsByTypeAndPId(int type, int pId, int skip, int limit) {
        List<Comments> comments = commentsMapper.findCommentsByTypeAndPId(type, pId, skip, limit);
        //拿到回复评论
        CommentsReply commentsReply;
        for (Comments comment : comments) {
            commentsReply = new CommentsReply();
            //拿到回复
            List<CommentsReply> commentsReplyList = commentReplyMapper.findCommentsReplyByPIdAndType(comment.getId(), MyConstants.commentsReplyType.commentType);
            //设置回复用户信息
            for (CommentsReply reply : commentsReplyList) {
                reply.setUser(userMapper.findUserById(reply.getUId()));
            }
            //设置回复评论集合
            comment.setCommentsReplyList(commentsReplyList);
            //拿到评论的用户信息
            comment.setUser(userMapper.findUserById(comment.getUId()));
        }
        return comments;
    }

    @Override
    public int updateCommentByIdAndStatus(int id, int status) {
        try {
            commentsMapper.updateCommentByIdAndStatus(id, status);
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    @Override
    public int deleteCommentById(int id) {
        try {
            commentsMapper.deleteCommentById(id);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public String commentCourseByComment(Comments comments) {
        try{
            comments.setCreateTime(new Date());
            commentsMapper.addComment(comments);
            return "评论成功!";
        }catch (Exception e){
            return "评论失败!";
        }

    }



}
